//
//  用户行为.m
//  xinhua_sx
//
//  Created by love on 14-10-22.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "用户行为.h"
#import "AFNetworking.h"
#import "XHAppDelegate.h"
@implementation butoncl

-(void)postString:(NSString *)gggg fff:(NSString*)fffff
{
     NSString* uid = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"UID"]];
    
        NSString* codeID =@"048002";
        NSString* userId = uid;
        
        NSString* userName = @"" ;
    NSLog(@"uid1111 = %@",uid);
    if (![uid isEqualToString:@"(null)"])
    {
        XHAppDelegate *appDele = (XHAppDelegate*)[[UIApplication sharedApplication]delegate];
        userName  = appDele.dic[@"name"];
        NSLog(@"username11 = %@",userName);
        if ([userName isEqualToString:@"(null)"])
            NSLog(@"222");
            userName = @"";
        
    }
    else
    {
        uid = @"";
        userName = @"" ;
    }
        NSString* IP = @"192.168.1.12";
        NSString* equipmentId = [self generateUuidString];
        NSString* writableyes = @"1";
        NSString* OSVersion = [NSString stringWithFormat:@"%f",[[[UIDevice currentDevice] systemVersion] floatValue]];
        NSString* APIVersion = @"";
        // NSString* identifierNumber = [[UIDevice currentDevice] uniqueIdentifier];
        //NSLog(@"手机序列号: %@",identifierNumber);
        //手机别名： 用户定义的名称
        NSString* userPhoneName = [[UIDevice currentDevice] name];
        NSLog(@"手机别名: %@", userPhoneName);
        //设备名称
        NSString* deviceName = [[UIDevice currentDevice] systemName];
        NSLog(@"设备名称: %@",deviceName );
        //手机系统版本
        NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
        NSLog(@"手机系统版本: %@", phoneVersion);
        //手机型号
        NSString* phoneModel = [[UIDevice currentDevice] model];
        NSLog(@"手机型号: %@",phoneModel );
        //地方型号  （国际化区域名称）
        NSString* localPhoneModel = [[UIDevice currentDevice] localizedModel];
        NSLog(@"国际化区域名称: %@",localPhoneModel );
        
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        // 当前应用名称
        NSString *appCurName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
        NSLog(@"当前应用名称：%@",appCurName);
        // 当前应用软件版本  比如：1.0.1
        NSString *appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        NSLog(@"当前应用软件版本:%@",appCurVersion);
        // 当前应用版本号码   int类型
        NSString *appCurVersionNum = [infoDictionary objectForKey:@"CFBundleVersion"];
        NSLog(@"当前应用版本号码：%@",appCurVersionNum);
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
        NSString* sting = [NSString stringWithFormat:@"%s/xinhua/m/userBehavior",HTTP];
        CGRect rect = [[UIScreen mainScreen] bounds];
        NSString* width = [NSString stringWithFormat:@"%d",(NSInteger)rect.size.width];
        NSString* height = [NSString stringWithFormat:@"%d",(NSInteger)rect.size.height];
        NSString* createTime;
    NSLog(@"with = %@  he = %@",width,height);
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"YYYYMMddHHmmss"];
    
    createTime=[dateformatter stringFromDate:senddate];
   
    NSLog(@"data = %@",createTime);
   

    
    
  
   
     XHAppDelegate *appDele = (XHAppDelegate*)[[UIApplication sharedApplication]delegate];
    NSLog(@"333334");
        NSDictionary* dic = @{@"appID":APPID,@"userID":uid,@"userName":userName,@"userIP":IP,@"appVersion":appCurVersionNum,@"appStoreID":@"001",@"deviceID":equipmentId,@"deviceIMSI":@"",@"mobileOS":@"01",@"deviceOSVersion":OSVersion,@"deviceModel":phoneModel,@"deviceBrand":@"iPhone",@"width":width,@"height":height,@"netWorkType":@"",@"operateType":gggg,@"createTime":createTime,@"operateObjID":fffff,@"sessionID":@"",@"serviceParm":@"",@"longitude":appDele.lg,@"latitude":appDele.la};
    NSLog(@"3333");
      NSString* sgt = [NSString stringWithFormat:@"appId=%@&userId=%@&userName=%@&IP=%@&versionNumber=%@&appStoreID=&equipmentId=%@&headingCode=&mobileOS=IOS&OSVersion=%@&phoneModel=%@&phoneBand=iPhone&width=%@&height=%@&networkType=&operateType=%@&createTime=%@&operateObjID=&sessionID=&serviceParm=&longitude=&latitude=",APPID,uid,userName,IP,appCurVersionNum,equipmentId,OSVersion,phoneModel,width,height,gggg,createTime];
    NSURL *url=[NSURL URLWithString:sting];//创建URL
    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];//通过URL创建网络请求
    [request setTimeoutInterval:30];//设置超时时间
    [request setHTTPMethod:@"POST"];//设置请求方式
    [request setHTTPBody:[sgt dataUsingEncoding:NSUTF8StringEncoding]];
    NSError *err;
   // NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];
    //NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    
   // NSLog(@"\n string:%@\n",str);
    [manager POST:sting parameters:dic success:^(AFHTTPRequestOperation*operation, id responseObject) {
            id jsonObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                            options:NSJSONReadingAllowFragments
                                                              error:nil];
            NSLog(@"jsonvvvvvv = %@",jsonObject);
            NSLog(@"resultvvvv = %@",jsonObject[@"message"]);
            NSLog(@"resultvvvv = %@",jsonObject[@"result"]);
            if ([jsonObject[@"result"] isEqualToString:@"success"])
            {
                NSLog(@"vvvvvvv");
                
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
            NSLog(@"error2 = %@",error);
        }];

        
    
    
}
- (NSString *)generateUuidString//UUID
{
    
    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
    NSString *uuidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid));
    
    CFRelease(uuid);
    
    return uuidString;
}
@end
